package samsung.sds.survey.surveyManagement.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import samsung.sds.survey.surveyManagement.model.QuestionEntity;
import samsung.sds.survey.surveyManagement.model.SurveyEntity;
import samsung.sds.survey.surveyManagement.service.SurveyDesignService;
import samsung.sds.survey.surveyManagement.service.SurveyManagementService;
import samsung.sds.system.common.baseQuery.model.PageData;
import samsung.sds.system.common.model.QueryParam;
import samsung.sds.system.utils.CommonTools;
import samsung.sds.system.utils.Constants;

/**
 *  @author deng1.pan
 *  @createDate 2016-01-08
 *  
 *  问卷管理功能模块的控制器
 */
@Controller
@RequestMapping("/survey/surveyManagement")
public class SurveyManagementController {

	@Autowired
	private SurveyManagementService surveyManagementServiceImpl;
	
	@Autowired
	private SurveyDesignService surveyDesignServiceImpl;
	
	 
	/**
	 * 跳转到投票管理模块的主界面
	 * @return
	 */
	@RequestMapping(value = "/surveyList", method = RequestMethod.POST)
	public String findSurveys(HttpServletRequest request, ModelMap modelMap) {
		String pageType = request.getParameter("pageType");
		modelMap.addAttribute("pageType", pageType);
		return "/survey/surveyManagement/surveyList";
	}

	/**
	 * 实现问卷列表的分页查询
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/listPageData", method = RequestMethod.POST)
	@ResponseBody
	public PageData<SurveyEntity> listPageData(HttpServletRequest request) {
		QueryParam param = CommonTools.getQueryParam(request);
		return surveyManagementServiceImpl.queryListPage(param);
	}
	
	/**
	 * 校验当前做新增和修改的用户跟问卷的create_User属性是否一致
	 * @param request
	 * @param survey_Id
	 * @param queryPersonInfo
	 * @return
	 */
	@RequestMapping(value = "/validateCreateUser", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> validateCreateUser(HttpServletRequest request,
			String survey_Id, String queryPersonInfo) {

		Map<String, Object> infomap = new HashMap<String, Object>();
		// 验证当前用户和问卷的createUser是否相符
		SurveyEntity survey = surveyManagementServiceImpl.findEntityById(survey_Id);
		String user = (String) request.getSession().getAttribute(
				"SESSION_USERNAME");
		if (!user.equals(survey.getCreate_User())) {
			infomap.put("validateOK", Constants.FAILURE_CODE);
		} else {
			infomap.put("validateOK", Constants.SUCCESS_CODE);
		}
		
		// 获取问卷人员信息
		if ("yes".equals(queryPersonInfo)) {
			int votedPersonCount =surveyManagementServiceImpl.getSurveyInfo(new String(survey_Id)).getVote_num();
			int invitationPersonsCount = surveyManagementServiceImpl.getSurveyInfo(new String(survey_Id)).getAsk_num();
			infomap.put("invitationPersonsCount", invitationPersonsCount);
			infomap.put("votedPersonCount", votedPersonCount);
		}

		return infomap;
	}

	/**
	 * 跳转到添加问卷的页面
	 * @param request
	 * @param survey_Id
	 * @param currentPage
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/surveyDetails", method = RequestMethod.POST)
	public String surveyDetails(HttpServletRequest request, String survey_Id,ModelMap modelMap) {
		
		// 验证当前问卷是否包含Question
		if (survey_Id != "") {
			List<QuestionEntity> list = surveyDesignServiceImpl.getQuestionEntity(new String(survey_Id));
			if (list.size() == 0) {
				modelMap.addAttribute("containQuestions", "no");
			} else {
				modelMap.addAttribute("containQuestions", "yes");
			}
		}
		SurveyEntity survey = surveyManagementServiceImpl.findEntityById(survey_Id);
		modelMap.addAttribute("survey", survey);
		return "/survey/surveyManagement/addOrUpdateSurvey";
	}

	/**
	 * 修改或新增问卷
	 * @param request
	 * @param model
	 * @param survey
	 * @return
	 */
	@RequestMapping(value = "/addOrUpdateSurvey", method = RequestMethod.POST)
	@ResponseBody
	public String addOrUpdateUser(HttpServletRequest request, SurveyEntity survey) {
		//检查参数
		String opType = request.getParameter("optype");
		if (survey == null || opType == null) {
			return Constants.ERRMSG_PARAM_ERROR;
		}
		//执行新建
		if (opType.equalsIgnoreCase("create")) {
			survey.setSurvey_Id(CommonTools.getKeyUUID());
			int rlt = surveyManagementServiceImpl.insertEntity(survey);
			if (rlt == 1) {
				return Constants.SUCCESS_CODE;
			} else {
				return Constants.FAILURE_CODE;
			}
		//执行修改
		} else if (opType.equalsIgnoreCase("update")) {
			int rlt = surveyManagementServiceImpl.updateEntity(survey);
			if (rlt == 1) {
				return Constants.SUCCESS_CODE;
			} else {
				return Constants.FAILURE_CODE;
			}
		
		} else if(opType.equalsIgnoreCase("copy")){
			String create_User =survey.getCreate_User();
			String surveyIdOld = survey.getSurvey_Id();
			survey.setSurvey_Id(CommonTools.getKeyUUID());
			String surveyIdNew = survey.getSurvey_Id();
			Date dt=new Date();
		    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
			survey.setEnd_Date(sdf.format(dt));
			
			int rltOfSurvey = surveyManagementServiceImpl.insertEntity(survey);
			boolean rltOfQuestion = surveyManagementServiceImpl.copyQuestion(surveyIdOld,surveyIdNew,create_User);
			
			if(rltOfSurvey == 1|| rltOfQuestion==true ){
				return Constants.SUCCESS_CODE;
			}else
			{   
				return Constants.FAILURE_CODE;
			}
		}
		//参数不匹配则返回错误信息
		else {
			return Constants.ERRMSG_PARAM_ERROR;
		}
	}

	/**
	 * 删除问卷
	 * @param request
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/deleteSurvey", method = RequestMethod.POST)
	@ResponseBody
	public String deleteSurvey(@RequestBody String id) {
		return surveyManagementServiceImpl.deleteSurvey(id);
	}

	/**
	 * 弹出发布窗口
	 * @param model
	 * @param survey
	 * @return
	 */
	@RequestMapping(value = "/popupSurveyRelease_sitemesh_popup", method = RequestMethod.POST)
	public String popupTest(SurveyEntity survey) {
		return "/survey/surveyManagement/addOrUpdateSurvey";
	}

	/**
	 * 对投票中的问卷停止投票
	 * @param request
	 * @param model
	 * @param survey
	 * @return
	 */
	@RequestMapping(value = "/stopVote", method = RequestMethod.POST)
	@ResponseBody
	public String stopVote(SurveyEntity survey) {
		//获取update的结果
		int rlt = surveyManagementServiceImpl.stopVote(survey);
		//返回值为1时
		if (rlt == 1) {
			return Constants.SUCCESS_CODE;
		//返回值不为1时
		} else {
			return Constants.FAILURE_CODE;
		}
	}
	
}
